__author__ = 'Apple'
#coding:utf-8
from flask import session
from flask import request
from flask import redirect
from flask import render_template
import time
import libs.weixinApi.get_openid_oauth2 as get_openid
import models.biding as biding
import models.stuinfo as stuinfo
import models.material as material
import models.note as note_content
import models.read_jounel as journal
import libs.util.leave_HMS_of_time as trans_time

def read_statistics_func(app):
    @app.route('/note/read')
    def read_waite_callback():
        code = request.args.get('code')
        state = request.args.get('state')

        openid = get_openid.get_openid(code)['openid']
        stu_id = biding.find_stuid(openid)['stu_id']
        # openid = 'dsaffsda'
        # stu_id = '121270026'
        name = stuinfo.find_name(stu_id)

        session['stu_id'] = stu_id
        session['name'] = name

        material_topic_id = long(note_content.new_page_view(stu_id, state))
        material_id = material.get_latest_material(material_topic_id)
        content = material.get_content_material_by_id(material_id)
        return render_template('notification.html',title=content["title"]
                               , author = content["author"], content = content["content"]
                               , date = trans_time.leave_hms(content["date"]))

    @app.route("/note/show-online")
    def online_f():
        stu_id = session['stu_id']
        note_id = request.args.get("note_id")
        result = journal.new_alive_journal(stu_id,note_id )
        return str(result)

    @app.route("/note/pointer-record", methods=['POST'])
    def pointer_f():
        stu_id = session['stu_id']
        note_id = request.args.get("note_id")
        pointer_record = request.form['record']
        user_agent = request.form['userAgent']
        result = journal.new_pointer_journal(note_id, stu_id, pointer_record, user_agent)
        return str(result)